/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Administrador.java
 *
 * Created on 18/02/2011, 10:48:13 AM
 */
package puntoventa;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import java.sql.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.view.JasperViewer;

/**
 *
 * @author Lab
 */
public class Administrador extends javax.swing.JFrame {

    private DefaultTableModel modelo = new DefaultTableModel();

    /** Creates new form Administrador */
    public Administrador() {
        initComponents();
        this.setLocationRelativeTo(this);
        modelo = (DefaultTableModel) this.tablaAdmin.getModel();
        mostrarTabla();

        int delay = 1;   // delay for 5 sec.
        int period = 1;  // repeat every sec.


        // Get rotation the local date and time .
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(
                new TimerTask() {

                    public void run() {

                        Date.setText(Main.ObtenerHora());
                        // Set the variable in Date JLabel
                    }
                }, delay, period);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        AgregarP = new javax.swing.JButton();
        ModificarP = new javax.swing.JButton();
        EliminarP = new javax.swing.JButton();
        BuscarP = new javax.swing.JButton();
        SalirB = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tablaAdmin = new javax.swing.JTable();
        Date = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu3 = new javax.swing.JMenu();
        Inic_Vent = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        AgregarP.setText("Agregar Producto");
        AgregarP.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AgregarPActionPerformed(evt);
            }
        });

        ModificarP.setText("Modificar Producto");
        ModificarP.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ModificarPActionPerformed(evt);
            }
        });

        EliminarP.setText("Eliminar Producto");
        EliminarP.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                EliminarPActionPerformed(evt);
            }
        });

        BuscarP.setText("Buscar Producto");
        BuscarP.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BuscarPActionPerformed(evt);
            }
        });

        SalirB.setText("Salir");
        SalirB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SalirBActionPerformed(evt);
            }
        });

        tablaAdmin.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID", "Nombre", "Proveedor", "Precio", "Stock"
            }
        ));
        tablaAdmin.setEnabled(false);
        jScrollPane1.setViewportView(tablaAdmin);

        Date.setText("YYYY/MM/DD HH:MM:SS");

        jLabel1.setFont(new java.awt.Font("Lucida Grande", 0, 18));
        jLabel1.setText("Administrador");

        jMenu3.setText("Ventas");

        Inic_Vent.setText("Iniciar Ventas...");
        Inic_Vent.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Inic_VentActionPerformed(evt);
            }
        });
        jMenu3.add(Inic_Vent);

        jMenuBar1.add(jMenu3);

        jMenu2.setText("Opciones Administrativas");

        jMenuItem1.setText("Añadir cuenta de usuario...");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem1);

        jMenuItem2.setText("Imprimir reporte de Inventario...");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem2);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(EliminarP, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 181, Short.MAX_VALUE)
                            .addComponent(AgregarP, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 181, Short.MAX_VALUE)
                            .addComponent(SalirB, javax.swing.GroupLayout.DEFAULT_SIZE, 181, Short.MAX_VALUE)
                            .addComponent(BuscarP, javax.swing.GroupLayout.DEFAULT_SIZE, 181, Short.MAX_VALUE)
                            .addComponent(ModificarP, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 181, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 454, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 417, Short.MAX_VALUE)
                        .addComponent(Date)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(Date)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(AgregarP, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(ModificarP, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(EliminarP, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 50, Short.MAX_VALUE)
                        .addComponent(BuscarP, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(36, 36, 36)
                        .addComponent(SalirB, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 307, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void Inic_VentActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Inic_VentActionPerformed
        try {
            DriverManager.registerDriver(new org.gjt.mm.mysql.Driver());
            Connection conexion = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/PDV", "root", "");
            Statement s = (Statement) conexion.createStatement();
            s.executeUpdate("delete from compras");
            conexion.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("Tabla C");

        Ventas_Carrito Venta = new Ventas_Carrito();
        Venta.setVisible(true);
        this.dispose();
    }//GEN-LAST:event_Inic_VentActionPerformed

    private void AgregarPActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AgregarPActionPerformed
        Agregar_Item Nprod = new Agregar_Item();
        Nprod.setVisible(true);
        this.dispose();
    }//GEN-LAST:event_AgregarPActionPerformed

    private void ModificarPActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ModificarPActionPerformed
        Modificar_Item Mprod = new Modificar_Item();
        Mprod.setVisible(true);
        this.dispose();
    }//GEN-LAST:event_ModificarPActionPerformed

    private void EliminarPActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_EliminarPActionPerformed
        Eliminar_Item Eprod = new Eliminar_Item();
        Eprod.setVisible(true);
        this.dispose();
    }//GEN-LAST:event_EliminarPActionPerformed

    private void SalirBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SalirBActionPerformed
        Inicio Ini = new Inicio();
        Ini.setVisible(true);
        this.dispose();
    }//GEN-LAST:event_SalirBActionPerformed

    private void BuscarPActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BuscarPActionPerformed
        Buscar_Item Busqueda = new Buscar_Item();
        Busqueda.setVisible(true);
        this.dispose();
    }//GEN-LAST:event_BuscarPActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        NuevaCuenta n = new NuevaCuenta();
        n.setVisible(true);
        this.dispose();

    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        try {
            ImprReport();
        } catch (JRException ex) {
            Logger.getLogger(Administrador.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Administrador.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Administrador().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton AgregarP;
    private javax.swing.JButton BuscarP;
    private javax.swing.JLabel Date;
    private javax.swing.JButton EliminarP;
    private javax.swing.JMenuItem Inic_Vent;
    private javax.swing.JButton ModificarP;
    private javax.swing.JButton SalirB;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tablaAdmin;
    // End of variables declaration//GEN-END:variables

    public void mostrarTabla() {
        try {
            DriverManager.registerDriver(new org.gjt.mm.mysql.Driver());
            Connection conexion = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/PDV", "root", "");
            Statement s = (Statement) conexion.createStatement();
            ResultSet rs = s.executeQuery("select * from Productos");
            Object[] fila = new Object[5];
            while (rs.next()) {
                fila[0] = rs.getString("ID");
                fila[1] = rs.getString("Nombre");
                fila[2] = rs.getString("Proveedor");
                fila[3] = rs.getString("Precio");
                fila[4] = rs.getString("Stock");

                modelo.addRow(fila);
                this.tablaAdmin.setModel(modelo);
            }
            conexion.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void ImprReport() throws JRException, SQLException {
        Map mapa = new HashMap();
        mapa.put("suma", 33);
        Connection c = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/pdv","root","");
        // TODO code application logic here
        JasperReport reporte = JasperCompileManager.compileReport("reporte/unnombre.jrxml");
        JasperPrint print = JasperFillManager.fillReport(reporte, mapa, c);
        JasperViewer visor = new JasperViewer(print);
        visor.setVisible(true);

    }
    }
